<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>获取课表</title>
<style>
 body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;margin:30px}
 input{width:60px;padding:6px}
 button{padding:6px 12px;margin-left:6px}
 pre{background:#f7f7f7;padding:12px;white-space:pre-wrap;word-break:break-all}
</style>
</head>
<body>
  <h2>获取课表</h2>
  <label>第 <input type="number" id="zc" min="1" max="25"> 周</label>
  <button onclick="query()">刷新/查询</button>
  <div id="result">正在计算周次…</div>

<script>
/* ---------- 计算当前是第几周 ---------- */
function autoWeek() {
  // 9 月 1 日（含）开始算第 1 周
  const start = new Date(new Date().getFullYear(), 8, 1); // 8 代表 9 月
  const today = new Date();
  return Math.max(1, Math.floor((today - start) / (7 * 24 * 60 * 60 * 1000)) + 1);
}

/* ---------- 拉课表 ---------- */
async function query() {
  const zc = document.getElementById('zc').value;
  document.getElementById('result').textContent = `正在拉取第 ${zc} 周课表…`;
  try {
    const res = await fetch(`/api/kb?zc=${encodeURIComponent(zc)}`);
    const text = await res.text();
    document.getElementById('result').insertAdjacentHTML('beforeend', text);
  } catch (e) {
    document.getElementById('result').textContent = '失败：' + e;
  }
}

/* ---------- 页面加载时自动设置并查询 ---------- */
window.addEventListener('load', () => {
  document.getElementById('zc').value = autoWeek();
  query();
});
</script>
</body>
</html>